<template>
  <div>
    <div class="login">
      <h1>登录</h1>
      <!-- from表单 -->
      <!-- rules属性: 表单验证规则 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" show-password></el-input>
        </el-form-item>
         <el-form-item label="验证码" prop="password">
          <el-row :gutter="10">
            <el-col :span="15">
               <el-input v-model="ruleForm.code"></el-input>
            </el-col>
            <el-col :span="8">
              <img :src="url" alt="" @click="getCaptchaImage('ruleForm')" style="width: 100%;height: 40px;">
            </el-col>
          </el-row>
         
          
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
import { login, captchaImage } from "@/api/login"
export default {
  data() {
    return {
      url:"",
      ruleForm: {
        username: 'admin01',
        password: '123456',
        uuid:''

      },
      rules: {
        username: [
          // trigger什么时候校验 blur失去焦点的时候 change：数据改变的时候
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        code:[
          { required: true, message: '验证码不能为空', trigger: 'change' }
        ]
      }
    };
  },
  created(){
    this.getCaptchaImage()
  },
  methods: {
    getCaptchaImage(){
      captchaImage().then(res =>{
        console.log(res);
        this.url = "data:image/gif;base64," + res.img
        this.ruleForm.uuid = res.uuid
      })
      
    },
    submitForm(formName) {
      // validate()将from校验一遍， valid参数返回true/false，表示是否通过校验
      this.$refs[formName].validate((valid) => {
        // 如果通过校验
        if (valid) {
          // 调接口 ===== 登录跳转
          // login(this.ruleForm).then(res => {
          //   console.log(res)
          //   if (res.code == 200) {
          //     this.$message({
          //       message: '登录成功',
          //       type: 'success'
          //     });
          //     this.$router.push({
          //       path: "/home"
          //     })
          //   }
          // })
          //调用vuex中封装好的登录方法 并传参
          // alert('submit!');
          this.$store.dispatch("gologin", this.ruleForm)
          // this.$store.dispatch("getUserInfo")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 500px;
  height: 300px;
  box-shadow: 1px 1px 8px rgba(0, 0, 0, 0.3);
  border-radius: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;

  h1 {
    padding: 10px 0 20px;
  }
}
</style>